package cn.virens.library.http;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xutils.http.RequestParams;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import cn.virens.library.activity.VApplication;
import cn.virens.library.uitls.CalendarUtil;


/**
 * Http请求封装-登录认证
 * Created by loioi on 2017/1/4.
 */
public class DefaultParams extends RequestParams {
    public DefaultParams(String uri) {
        super(VApplication.uri(uri));
        super.addHeader("Authorization", VApplication.token());
    }

    public void addBodyParameter(String name, Integer value) {
        if (value != null) super.addBodyParameter(name, value.toString());
    }

    public void addBodyParameter(String name, Long value) {
        if (value != null) super.addBodyParameter(name, value.toString());
    }

    public void addBodyParameter(String name, Double value) {
        if (value != null) super.addBodyParameter(name, value.toString());
    }

    public void addBodyParameter(String name, Float value) {
        if (value != null) super.addBodyParameter(name, value.toString());
    }

    public void addBodyParameter(String name, Boolean value) {
        if (value != null) super.addBodyParameter(name, value.toString());
    }

    public void addBodyParameter(String name, CharSequence cs) {
        if (cs != null) super.addBodyParameter(name, cs.toString());
    }

    public void addBodyParameter(String name, Date value) {
        if (value != null) super.addBodyParameter(name, CalendarUtil.format(value, CalendarUtil.YMD_HMS));
    }

    public void addBodyParameter(String name, Calendar value) {
        if (value != null) super.addBodyParameter(name, CalendarUtil.format(value, CalendarUtil.YMD_HMS));
    }

    public void addQueryStringParameter(String name, Object value) {
        if (value != null) super.addQueryStringParameter(name, value.toString());
    }

    public void setBodyContent(Map<String, Object> content) {
        this.setBodyContent(content != null ? new JSONObject(content) : null);
        this.setAsJsonContent(true);
    }

    public void setBodyContent(JSONObject content) {
        this.setBodyContent(content != null ? content.toString() : "{}");
        this.setAsJsonContent(true);
    }

    public void setBodyContent(JSONArray content) {
        this.setBodyContent(content != null ? content.toString() : "[]");
        this.setAsJsonContent(true);
    }
}
